package cn.e4j.modular.api.service;


import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.AfterSaleMapper;
import cn.e4j.modular.api.mapper.BusinessMapper;
import cn.e4j.modular.api.mapper.OuterUserMapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.common.mapper.SysAuthorityMapper;
import cn.e4j.modular.crm.entity.MmrllT;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AfterSaleService {


    @Autowired
    BusinessMapper businessMapper;

    @Autowired
    AfterSaleMapper saleMapper;

    @Transactional(readOnly = true)
    public List<Dto> getGoodsInfo(Dto indto){

        return businessMapper.getGoodsInfo(indto);
    }

    @Transactional(readOnly = true)
    public List<Dto> getMMRG(Dto inDto) {

        return InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.AfterSaleMapper.getMMRG",inDto);
    }

    @Transactional(readOnly = true)
    public List<Dto> getMMRD(Dto inDto) {
        QueryWrapper<MmrllT> wrapper = new QueryWrapper<>();
        wrapper.select("MMRLL001","MMRLL003");
        List<MmrllT> list = new MmrllT().selectList(wrapper);
        List<Dto> result = new ArrayList<>();
        for (MmrllT m:list){
            Dto dto = new Dto();
            inDto.put("MMRL001",m.getMMRLL001());
            List<Dto> mmrd = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.AfterSaleMapper.getMMRD",inDto);
            dto.put("MMRL001",m.getMMRLL001());
            dto.put("MMRL003",m.getMMRLL003());
            dto.put("COUNT", ToolUtil.isNotEmpty(mmrd)?mmrd.size():"");
            result.add(dto);
        }
        return result;
    }

    @Transactional(readOnly = true)
    public List<Dto> getMMRK(Dto inDto) {
        inDto.put("LANG", LangUtils.getLang(inDto.getString("LANG")));
        List<Dto> mmrk = saleMapper.getMMRK(inDto);
        int year = LocalDate.now().getYear();
        List<Dto> list = new ArrayList<>();
        for (Dto d:mmrk){
            Dto dto = new Dto();
            dto.put("ENT",inDto.getString("ENT"));
            dto.put("YEAR",year);
            dto.put("MMRK003",d.getString("MMRK003"));
            dto.put("MMRJL004",d.getString("MMRJL004"));
            d.put("list",InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.AfterSaleMapper.getStarCount",dto));

            list.add(d);
        }
        return list;
    }

    @Transactional(readOnly = true)
    public List<Dto> getTime(Dto inDto) {
        List<Dto> timeList = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.AfterSaleMapper.getTimeInfo",inDto);
        for (Dto time:timeList){
            Integer mmrg101 = time.getInteger("MMRG101");
            Integer mmrg104 = time.getInteger("MMRG104");

            time.put("serverTime",(mmrg101==null?0:mmrg101)+(mmrg104==null?0:mmrg104));
        }
        return timeList;
    }

    @Transactional(readOnly = true)
    public List<Dto> getCOUNT(Dto inDto) {
        List<Dto> countList = saleMapper.getCOUNT(inDto);
        return countList;
    }

    @Transactional(readOnly = true)
    public List<Dto> getTodayTask(Dto dto) {

        return InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.AfterSaleMapper.getTodayTask",dto);
    }
}
